package com.zcah.sensitive.scan;



import com.zcah.sensitive.dao.SenInfoMetaRepository;
import com.zcah.sensitive.entity.SenInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

//@Component
//public class HdfsScan {
//    @Autowired
//    @Qualifier("myFileSystem")
//    private FileSystem fileSystem;
//
//
//    @Autowired
//    private SenInfoMetaRepository senInfoMetaRepository;
//
//    @Autowired
//    private InfoFound infoFound;
//
//
//    @Scheduled(fixedRate = 60 * 1000)
//    public void hdfslist() throws IOException{
//        System.out.println("schedule scan");
////        String hdfsPath = "file:///root/IdeaProjects/sensitive-web/src/main/resources/usersdata1.txt";
////        Map<String, Integer> sensitiveResult = infoFound.getSensitiveInfo(hdfsPath);
////        for (Map.Entry<String, Integer> result : sensitiveResult.entrySet()) {
////            System.out.println(result.getKey() + result.getValue());
////        }
//
//        FileStatus fs[];
//        if (fileSystem == null) {
//            System.out.println("Null");
//        }
//        try {
//            fs = fileSystem.listStatus(new Path("/"));
//            this.browse(fs);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//
//    private void browse(FileStatus[] status) throws IOException {
//        System.out.println("starting browse!!!");
//        for (FileStatus fileStatus : status) {
//            if (fileStatus.isDirectory()) {
//                FileStatus[] subStatus = fileSystem.listStatus(fileStatus.getPath());
//                browse(subStatus);
//            } else if (fileStatus.isFile()) {
//                Date now = new Date();
//                SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd");
//                SimpleDateFormat time_format = new SimpleDateFormat("HH");
//                Set<String> hostSet = new HashSet<>();
//                BlockLocation myBlocks[] = fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
//                for (BlockLocation b : myBlocks) {
//                    for (String host : b.getHosts()) {
//                        hostSet.add(host);
//                    }
//                }
//                String hdfsPath = "hdfs://192.168.101.114:8020/demo/data/usersdata.txt";
////                String hdfsPath = fileStatus.getPath().toString();
//                System.out.println("hdfsPath"+hdfsPath);
//                if (hdfsPath.contains(".")) {
//                    String[] splitName = fileStatus.getPath().getName().split("\\.");
//                    String extension = splitName[splitName.length - 1];
//                    if (extension.equalsIgnoreCase("csv") | extension.equalsIgnoreCase("txt")) {
//                        System.out.println("PATH:" + hdfsPath);
//                        Map<String, Integer> sensitiveResult = infoFound.getSensitiveInfo(hdfsPath);
//                        for (Map.Entry<String, Integer> result : sensitiveResult.entrySet()) {
//                            for (String host : hostSet) {
//                                SenInfo senInfo = new SenInfo();
//                                senInfo.setHdfsPwd(hdfsPath);
//                                senInfo.setDate(date_format.format(now));
//                                senInfo.setTime(time_format.format(now));
//                                senInfo.setServerIP(host);
//                                senInfo.setSenType(result.getKey());
//                                senInfo.setSenSize(result.getValue());
//                                senInfoMetaRepository.saveAndFlush(senInfo);
//                            }
//                        }
//                    }
//                }
//
//            }
//        }
//    }
    /*private void browse(FileStatus[] status) throws IOException {
        for (FileStatus fileStatus : status) {
            HdfsMeta hdfs = new HdfsMeta();
            Long datenow = new java.util.Date().getTime();
            hdfs.setFound_datetime(new Date(datenow));
            hdfs.setAccess_time(new Date(fileStatus.getAccessTime()));
            hdfs.setBlocksize(fileStatus.getBlockSize());
            hdfs.setG_group(fileStatus.getGroup());
            hdfs.setPath(fileStatus.getPath().toString());
            hdfs.setModification_time(new Date(fileStatus.getModificationTime()));
            hdfs.setOwner(fileStatus.getOwner());
            hdfs.setPermission(fileStatus.getPermission().toString());
            hdfs.setReplication(fileStatus.getReplication());
            hdfs.setSymlink(fileStatus.isSymlink());
            if (fileStatus.isDirectory()) {
                hdfs.setDirectory(true);
                FileStatus[] subStatus = this.fileSystem.listStatus(fileStatus.getPath());
                browse(subStatus);
            } else if (fileStatus.isFile()) {
                hdfs.setDirectory(false);
                String[] splitPath = fileStatus.getPath().toString().split("/");
                int splitSize = splitPath.length;
                String name = splitPath[splitSize - 1];
                hdfs.setName(name);
                if (name.contains(".")) {
                    String[] splitName = fileStatus.getPath().getName().split("\\.");
                    String extension = splitName[splitName.length - 1];
                    hdfs.setExtension(extension);
                }
                Set<String> hostSet = new HashSet<String>();
                BlockLocation myBlocks[] = this.fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
                for (BlockLocation b : myBlocks) {
                    for (String host : b.getHosts()) {
                        hostSet.add(host);
                    }
                }
                List<String> hostsList = new ArrayList<String>(hostSet);
                hdfs.setHosts(hostsList.toString());
            }
            hdfsMetaRepository.save(hdfs);
        }

    }*/
//}
